<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	/**
	 * Init doctype
	 * (not used in layout, but affects appendStylesheet method)
	 */
	protected function _initDoctype()
	{
		$this->bootstrap('view');
		$view = $this->getResource('view');
		$view->doctype('XHTML1_STRICT');
	}

	protected function _initFrontController()
	{
		$front = Zend_Controller_Front::getInstance();
//		$front->setControllerDirectory(array(
//		    'default' => APPLICATION_PATH . '/controllers',
//		));

		$front->setControllerDirectory(array(
		    'default' 	=> APPLICATION_PATH . '/modules/default/controllers',
			'admin'		=> APPLICATION_PATH . '/modules/admin/controllers',
		));

//		$front->setModuleControllerDirectoryName('controllers');
		$front->addModuleDirectory(APPLICATION_PATH . '/modules');


		$front->setDefaultModule('default');

		$router = $front->getRouter();

		$router->addRoute('cards_by_category',
				new Zend_Controller_Router_Route_Regex(
						'category/(\d+)(?:/page/(\d+))?(?:/card/(\d+))?$',
						array(
								'controller' => 'category',
								'action' => 'index',
								'page' => '1',
								'card' => null
						),
						array(
								1 => 'category',
								2 => 'page',
								3 => 'card',
						),
						'category/%d/page/%d'
				)
		);

		$router->addRoute('card_by_id',
				new Zend_Controller_Router_Route_Regex(
						'card/(\d+)$',
						array(
								'controller' => 'card',
								'action' => 'index'
						),
						array(
								1 => 'card',
						)
				)
		);

//		$router->addRoute('restaurants_by_id',
//				new Zend_Controller_Router_Route_Regex(
//						'restaurants/(\d+)$',
//						array(
//								'controller' => 'restaurants',
//								'action' => 'view'
//						),
//						array(
//								1 => 'restaurant_id',
//						)
//				)
//		);

		return $front;
	}

	protected function _initAutoload()
	{
		$autoloader = Zend_Loader_Autoloader::getInstance();
		$autoloader->setFallbackAutoloader(true);
		return $autoloader;
	}

	protected function _initConfig()
	{
		$oConfig = new Zend_Config_Ini('configs/application.ini');
		$oRegistry = Zend_Registry::getInstance();
		$oRegistry->set('config', $oConfig);
	}

	protected function _initTranslation()
	{
		$translate = new Zend_Translate('array', APPLICATION_PATH.'/languages/lang_en.php', 'en');
		Zend_Registry::set('Zend_Translate', $translate);
		// Tell all validation classes to use a specific translate adapter
		Zend_Validate_Abstract::setDefaultTranslator($translate);
		// Tell all form classes to use a specific translate adapter, as well as use
		// this adapter to translate validation error messages:
		Zend_Form::setDefaultTranslator($translate);
	}

	protected function _initSession()
	{
		$oRegistry = Zend_Registry::getInstance();
		$oConfig = $oRegistry->get('config')->session;
		Zend_Session::setOptions($oConfig->toArray());
		$oRegistry->set('Zend_Session_Namespace', new Zend_Session_Namespace());
	}
}
